﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JUtil;
using JUtil.CodeGenerator;

namespace CodeGenerator
{
    public class MasterViewGenerator
    {
        public MasterViewGenerator(CodeGenSetting CodeGenSetting)
        {
            this.DbSetting = CodeGenSetting.DbSetting;
            this.CSharpFileSetting = CodeGenSetting.CSharpFileSetting;
            this.RelatedSql = CodeGenSetting.RelatedSql;
            this.ExportDirectorySetting = CodeGenSetting.ExportDirectorySetting;

            this.RelatedSql.GetSelectDetailsVwSQL = string.Empty;
            this.RelatedSql.GetInsertSQL = string.Empty;
            this.RelatedSql.GetUpdateSQL = string.Empty;
        }

        DbSetting DbSetting;
        CSharpFileSetting CSharpFileSetting;
        RelatedSql RelatedSql;
        ExportDirectorySetting ExportDirectorySetting;

        private void ExportSqlFile(string filepath)
        {
            CodeGenBase generator = new CodeGenerator.SqlFile(RelatedSql);

            generator.Export(filepath);
        }

        private void ExportDalFile(string filepath)
        {
            CodeGenBase generator = new DalFile(
                                        DbSetting,
                                        CSharpFileSetting,
                                        RelatedSql);

            generator.Export(filepath);
        }

        private void ExportBllFile(string filepath)
        {
            CodeGenBase generator = new BllFile(
                                        DbSetting,
                                        CSharpFileSetting,
                                        RelatedSql);

            generator.Export(filepath);
        }

        private void ExportAspxCsFile(string filepath)
        {
            CodeGenBase generator = new MasterViewAspxCsFile(
                                        CSharpFileSetting);

            generator.Export(filepath);
        }

        private void ExportAspxFile(string filepath)
        {
            CodeGenBase generator = new MasterViewAspxFile(
                                        DbSetting,
                                        CSharpFileSetting,
                                        RelatedSql);

            generator.Export(filepath);
        }

        public void Export()
        {
            string pathSqlFile = JUtil.Path.File.GetAbsolutePath(
                                    ExportDirectorySetting.SqlFile,
                                    CSharpFileSetting.BLL_CLASS + ".sql");
            ExportSqlFile(pathSqlFile);

            string pathDalFile = JUtil.Path.File.GetAbsolutePath(
                                    ExportDirectorySetting.DalFile,
                                    CSharpFileSetting.DAL_CLASS + ".cs");
            ExportDalFile(pathDalFile);

            string pathBllFile = JUtil.Path.File.GetAbsolutePath(
                                    ExportDirectorySetting.BllFile,
                                    CSharpFileSetting.BLL_CLASS + ".cs");
            ExportBllFile(pathBllFile);

            string pathAspxCsFile = JUtil.Path.File.GetAbsolutePath(
                                    ExportDirectorySetting.AspxCsFile,
                                    CSharpFileSetting.ASPX_CLASS + ".aspx.cs");
            ExportAspxCsFile(pathAspxCsFile);

            string pathAspxFile = JUtil.Path.File.GetAbsolutePath(
                                    ExportDirectorySetting.AspxCsFile,
                                    CSharpFileSetting.ASPX_CLASS + ".aspx");
            ExportAspxFile(pathAspxFile);
        }
    }
}
